package com.java.sano.result;

import lombok.*;

import java.io.Serializable;
import java.sql.ResultSet;

@Data
@NoArgsConstructor
@Builder(toBuilder = true)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class Result<T> implements Serializable {
    private static final long serialVersionUID = -8376228737279020847L;
    //是否成功
    private Boolean success;
    //返回状态码
    private Integer code;
    //返回信息
    private String message;
    //layui统计记录数
    private Long count;
    //返回数据
    private T data;

    //创建ResultBean对象,接收数据
    public static<T> Result<T> build(T data) {
    //实例化Result对象
        Result<T> result=new Result<>();
        if (data != null) {
            result.setData(data);
        }
        return result;
    }

    /**
     * 操作成功
     * @param resultCodeEnum
     * @param data
     * @param <T>
     * @return
     */
    public static<T> Result<T> ok(ResultCodeEnum resultCodeEnum,T data){
        Result<T> result = build(data); //调用build方法
        result.setSuccess(true);    //操作成功，默认为true
        //SUCCESS(200, "成功"),
        result.setCode(resultCodeEnum.getCode());  //200
        result.setMessage(resultCodeEnum.getMessage()); //"成功"
        return result;
    }

    public static<T> Result<T> ok(ResultCodeEnum resultCodeEnum,Long count,T data){
        Result<T> result=ok(resultCodeEnum,data);
        result.setCount(count);
        return result;
    }




    public static<T> Result<T> fail(ResultCodeEnum resultCodeEnum,T data){
        Result<T> result = build(data);
        result.setSuccess(false);    //操作失败，默认为false
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }
    public static<T> Result<T> fail(){
        Result<T> result = new Result<>();
        result.setSuccess(false);    //操作失败，默认为false
        result.setCode(201);
        return result;
    }
}
